package branch_and_bound;

import gurobi.GRB;
import gurobi.GRBEnv;
import gurobi.GRBException;
import gurobi.GRBLinExpr;
import gurobi.GRBModel;
import gurobi.GRBVar;

public class BranchAndBound {
    public static void main(String[] args) {

        GRBEnv env;
        GRBModel model;
        try {

            // 构建模型
            env = new GRBEnv(true);
            env.start();
            model = new GRBModel(env);
            model.set("OutputFlag", "0");

            // 创建决策变量
            GRBVar[] x = model.addVars(3, GRB.BINARY);

            // 设置目标函数
            GRBLinExpr expr = new GRBLinExpr();
            expr.addTerms(new double[] { 4, 9, 6 }, x);
            model.setObjective(expr, GRB.MAXIMIZE);

            // 添加约束
            expr = new GRBLinExpr();
            expr.addTerms(new double[] { 5, 8, 6 }, x);
            model.addConstr(expr, GRB.LESS_EQUAL, 13, "c0");

            // model.optimize();
            model.update();

            // 求解
            Solver solver = new Solver(env, model);
            solver.solve();

        } catch (GRBException e) {
            System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage());
        }
    }
}
